//
//  ImageCropper.m
//  Created by http://github.com/iosdeveloper
//

#import "ImageCropper.h"
#import "PMGlobal.h"

@implementation ImageCropper

@synthesize scrollView, imageView;
@synthesize delegate;

- (id)initWithImage:(UIImage *)image {
	self = [super init];
	
	if (self) {
		[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES];
		
        UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 1024.0, 44.0)];
        UIImage *img = [[PMGlobal getInstance]getnavigationbarimage];
        [navigationBar setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];

		[navigationBar setTranslucent:YES];
		
        float offset = 35;
        scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(offset, 0, 1024-offset*2, 700)];
        scrollView.center = CGPointMake(1024/2, (768-44)/2);
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:scrollView];
        scrollView.top = navigationBar.bottom+10;
        scrollView.backgroundColor = [UIColor clearColor];
        //  scroll.center = CGPointMake(1024/2, (748-44)/2);
        scrollView.clipsToBounds = YES;
        scrollView.delegate = self;

        imageView = [[UIImageView alloc]initWithImage:image];
        imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
        imageView.clipsToBounds = YES;
        [scrollView addSubview:imageView];
        scrollView.contentSize = CGSizeMake(image.size.width, image.size.height);
        scrollView.contentOffset = CGPointMake(100, 100);
        /*
		UIButton *close = [UIButton buttonWithType:UIButtonTypeCustom];
        close.frame = CGRectMake(260, 0, 50, 30);
        close.layer.cornerRadius = 5;
        close.layer.borderWidth = 1;
        //close.layer.borderColor = [UIColor colorWithRed:100/255.0 green:100/255.0 blue:100/255.0 alpha:1].CGColor;
        
        close.center = CGPointMake(980, navigationBar.height/2);
        [close setTitle:@"返回" forState:UIControlStateNormal];
        close.titleLabel.font = [UIFont systemFontOfSize:15];
        close.backgroundColor = [UIColor blueColor];
        //[close setBackgroundColor:[UIColor colorWithRed:40/255.0 green:40/255.0 blue:40/255.0 alpha:1]];
        [close addTarget:self action:@selector(cancelCropping) forControlEvents:UIControlEventTouchUpInside];
        [navigationBar addSubview:close];
*/
        
        
		UINavigationItem *aNavigationItem = [[UINavigationItem alloc] initWithTitle:nil];
        UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
        img = [[UIHelper getInstance] getBundleImage:@"anniu.png"];
        [b setBackgroundImage:img forState:UIControlStateNormal];
        b.frame = CGRectMake(10, 10, img.size.width, img.size.height);
        [b setTitle:@"返回" forState:UIControlStateNormal];
        [b addTarget:self action:@selector(cancelCropping) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithCustomView:b];
        aNavigationItem.rightBarButtonItem = right;
    //	[aNavigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelCropping)]];
	//	[aNavigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(finishCropping)]];
		
		[navigationBar setItems:[NSArray arrayWithObject:aNavigationItem]];
		
		[[self view] addSubview:navigationBar];
        
//        navigationBar.
//        
//        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone  target:self action:@selector(close)];
//        
//        self.navigationItem.rightBarButtonItem = item;
        
	}
	
	return self;
}

- (void)cancelCropping {
	[delegate imageCropperDidCancel:self]; 
}

- (void)finishCropping {
	float zoomScale = 1.0 / [scrollView zoomScale];
	
	CGRect rect;
	rect.origin.x = [scrollView contentOffset].x * zoomScale;
	rect.origin.y = [scrollView contentOffset].y * zoomScale;
	rect.size.width = [scrollView bounds].size.width * zoomScale;
	rect.size.height = [scrollView bounds].size.height * zoomScale;
	
	CGImageRef cr = CGImageCreateWithImageInRect([[imageView image] CGImage], rect);
	
	UIImage *cropped = [UIImage imageWithCGImage:cr];
	
	CGImageRelease(cr);
	
	[delegate imageCropper:self didFinishCroppingWithImage:cropped];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
	return imageView;
}

- (void)dealloc {
}

@end